<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "banners".
 *
 * @property string $id
 * @property string $title
 * @property string $media_url
 * @property string $ad_id
 * @property string $create_time
 * @property string $link_url
 */
class Banners extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'banners';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ad_id', 'create_time'], 'integer'],
            [['title', 'media_url'], 'string', 'max' => 128],
            [['title'],'required','message'=>'标题不能为空'],
            [['link_url'], 'string', 'max' => 256]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'media_url' => '图片',
            'ad_id' => '广告位',
            'create_time' => '创建时间',
            'link_url' => '链接地址',
        ];
    }
    public function save($runValidation = true, $attributeNames = null){
        if($this->isNewRecord){
            $this->create_time=time();
        }
        return parent::save($runValidation, $attributeNames);
    }
    //获取广告位名称
    public function getAdvert($id){
        $arr=Advert::find()->where(['id'=>$id])->asArray()->one();
        return $arr['name'];
    }
}
